﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MINERP.MODEL.GenerateReports;
using MINERP.DAL.GenerateReports;
using MINERP.DAL;



namespace MINERP.BLL.GenerateReports
{
    public class ProductStatisticsBLL 
    {

        ProductStatisticsDAL SSDAL = new ProductStatisticsDAL();

        public List< ProductStatitics_Info> GetProductStatitics_Info(DateTime startTime, DateTime endTime)
        {
            List<ProductStatitics_Info> list = new List<ProductStatitics_Info>();
            DataTable  dt = GetProductPrice(startTime, endTime);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                ProductStatitics_Info productInfo = new ProductStatitics_Info();
                productInfo.Price = Convert.ToDecimal(dt.Rows[i]["Price"]);
                productInfo.ProductArea = Convert.ToDouble(dt.Rows[i]["ProductArea"]);
                productInfo.ProductName = dt.Rows[i]["ProductName"].ToString();
                productInfo.ProductNum = Convert.ToInt32(dt.Rows[i]["ProductNum"]);
                list.Add(productInfo);
            }
            return list;
        }
        #region 获取一段时间内所有的产品金额统计
        public DataTable GetProductPrice(DateTime startTime, DateTime endTime)
        {
            try
            {
                List<BillDetailEntity> ListProductDetial = GetAllBillDetail(startTime, endTime);
                var group = ListProductDetial.GroupBy(p => p.ProductId);
                DataTable productPriceDT = BillDataTable.CreateProductPrice();
                foreach (var g in group)
                {
                    DataRow row = productPriceDT.NewRow();
                    int num = 0;
                    double price = 0;
                    double area = 0;
                    string pn = g.First().ProductName;
                    foreach (var gg in g)
                    {
                        num++;
                        price += gg.BillDetailPrice;
                        area += ProductStatisticsDAL.GetAreaOfAOrderInfo(gg.OrderInfoCode);        //每个产品的总面积
                    }
                    row["ProductName"] = pn;
                    row["ProductNum"] = num;
                    row["ProductArea"] = area;                                              //这种产品的总面积
                    row["Price"] = price;
                    productPriceDT.Rows.Add(row);
                }
                return productPriceDT;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一段时间内所有的产品金额明细
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public List<BillDetailEntity> GetAllBillDetail(DateTime startTime, DateTime endTime)
        {
            try
            {
                List<Bill_Detail> ListBillDetail = SSDAL.GetListBillDetail(startTime, endTime);
                List<BillDetailEntity> ListBillProduct = new List<BillDetailEntity>();
                foreach (Bill_Detail bd in ListBillDetail)
                {
                    BillDetailEntity bill = new BillDetailEntity();
                    bill.BillDetailId = bd.BillDetailId;
                    double price = SSDAL.GetPriceOfAOrderInfo(bd.BillDetailId);
                    bill.BillDetailPrice = price;
                    Tb_Product product = SSDAL.GetProductByOrderInfoCode(bd.OrderInfoCode);
                    if (product == null)
                        continue;
                    bill.OrderInfoCode = bd.OrderInfoCode;
                    bill.ProductId = product.ProuctId;
                    bill.ProductName = product.ProductName;
                    ListBillProduct.Add(bill);
                }
                return ListBillProduct;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
